package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import com.qf.service.DtsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Cc
 * @version 1.0
 */

@Service
public class DtsCouponServiceImpl implements DtsCouponService {

    @Autowired
    private DtsCouponMapper dtsCouponMapper;



    @Override
    public IPage<DtsCoupon> page(String username, Integer page, Integer limit, String sort, String order) {
        return dtsCouponMapper.selectPage(new Page(page,limit),
                new QueryWrapper<DtsCoupon>()
                        .eq(!StringUtils.isEmpty(username),"name",username)
                        .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort)
        );
    }

    @Override
    public DtsCoupon findById(Integer id) {
       return dtsCouponMapper.selectById(id);
    }

    @Override
    public int add(DtsCoupon coupon) {
        return dtsCouponMapper.insert(coupon);
    }

    @Override
    public Map<String, Object> update(DtsCoupon dtsCoupon) {
        Integer id = dtsCoupon.getId();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        dtsCouponMapper.update(dtsCoupon,wrapper);
        Map<String,Object> map=new HashMap<>();
        map.put("id",dtsCoupon.getId());
        map.put("name",dtsCoupon.getName());
        map.put("desc",dtsCoupon.getDesc());
        map.put("tag",dtsCoupon.getTag());
        map.put("total",dtsCoupon.getTotal());
        map.put("discount",dtsCoupon.getDiscount());
        map.put("min",dtsCoupon.getMin());
        map.put("limit",dtsCoupon.getLimit());
        map.put("type",dtsCoupon.getType());
        map.put("status",dtsCoupon.getStatus());
        map.put("goodsType",dtsCoupon.getGoodsType());
        map.put("goodsValue",dtsCoupon.getGoodsValue());
        map.put("timeType",dtsCoupon.getTimeType());
        map.put("days",dtsCoupon.getDays());
        map.put("addTime",dtsCoupon.getAddTime());
        map.put("updateTime",dtsCoupon.getUpdateTime());
        return map;
    }

    @Override
    public void delete(Integer id) {
        dtsCouponMapper.updateDeleteById(id);
    }
}
